package com.arunsawad.ilertu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.entity.Emergency;

public class EmergenciesAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater;

	private List<Emergency> items = new ArrayList<Emergency>();

	public EmergenciesAdapter(Context context, List<Emergency> items) {
		layoutInflater = LayoutInflater.from(context);
		this.items = items;
	}

	public int getCount() {
		return items.size();
	}

	public Emergency getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		EmergenciesViewHolder holder;
		Emergency item = items.get(position);
		if (convertView == null) {
			convertView = layoutInflater.inflate(
					R.layout.lv_emergencies, null);
			holder = new EmergenciesViewHolder();
			holder.image = (ImageView) convertView
					.findViewById(R.id.emergencies_image);
			holder.tel = (TextView) convertView
					.findViewById(R.id.emergencies_txtTel);
			holder.name = (TextView) convertView
					.findViewById(R.id.emergencies_txtName);
			convertView.setTag(holder);
		} else {
			holder = (EmergenciesViewHolder) convertView.getTag();
		}
		holder.tel.setText(item.getTel());
		holder.name.setText(item.getName());
		if (item.getImage() != null) {
			holder.image.setImageBitmap(item.getImage());
		} else {
			// MY DEFAULT IMAGE
			holder.image.setImageResource(R.drawable.noimg_128);
		}
		return convertView;
	}

	static class EmergenciesViewHolder {
		ImageView image;
		TextView tel;
		TextView name;
	}
}